package coolq.yaozhenyong.controller;

import java.sql.Time;
import java.util.List;

import com.forte.qqrobot.anno.Filter;
import com.forte.qqrobot.anno.Listen;
import com.forte.qqrobot.anno.depend.Beans;
import com.forte.qqrobot.beans.messages.msgget.GroupMemberIncrease;
import com.forte.qqrobot.beans.messages.msgget.GroupMemberReduce;
import com.forte.qqrobot.beans.messages.types.MsgGetTypes;
import com.forte.qqrobot.sender.MsgSender;
import com.forte.qqrobot.utils.CQCodeUtil;

import coolq.yaozhenyong.bean.GradeAdmin;
import coolq.yaozhenyong.bean.GradeUserGoto;
import coolq.yaozhenyong.custom.HtmlAndQq;
import coolq.yaozhenyong.custom.SpringContextUtil;
import coolq.yaozhenyong.custom.TimeUtils;
import coolq.yaozhenyong.service.GradeAdminService;
import coolq.yaozhenyong.service.GradeUserGotoService;

@Beans
public class QQGroupGotoController {
	public GradeAdminService gService = SpringContextUtil.getBean(GradeAdminService.class);
	/****
	 * 进群欢迎语
	 * 进群修改群名片
	 */
	@Listen(MsgGetTypes.groupMemberIncrease)
	@Filter	
	public void groupindex(GroupMemberIncrease msg,CQCodeUtil cqCodeUtil,MsgSender msgsender) {
		try {
			//1.取出改群的相关设置
			List<GradeAdmin> grade = gService.query().eq("gradenumber", msg.getGroup()).list();
			GradeAdmin robot = new GradeAdmin();
			GradeAdmin gograde = new GradeAdmin();
			GradeAdmin gogradenikename = new GradeAdmin();
			for (GradeAdmin gradeAdmin : grade) {
				if(gradeAdmin.getCodes().equals("robot")&&gradeAdmin.getIsgoto()) {
					robot = gradeAdmin;
				}else if(gradeAdmin.getCodes().equals("gograde")&&gradeAdmin.getIsgoto()) {
					gograde = gradeAdmin;
				}else if(gradeAdmin.getCodes().equals("gogradenikename")&&gradeAdmin.getIsgoto()) {
					gogradenikename = gradeAdmin;
				}
			}
			//2. 判断机器人是否为启动状态 
			if(robot!=null) {
				GradeUserGotoService gotoservice = SpringContextUtil.getBean(GradeUserGotoService.class);
				
				//3.判断是否需要修改群名片
				if(gogradenikename!=null) {
					GradeUserGoto GradeUserGoto = gotoservice.query().eq("qunnumber", msg.getGroup()).eq("type","nicknamegrade").one();
					if(GradeUserGoto.getInputs()!=null) {
						msgsender.SETTER.setGroupCard(msg.getGroup(), msg.getBeOperatedQQ(), HtmlAndQq.msggotograde(GradeUserGoto.getInputs(),cqCodeUtil,msg));
					}
				}
				
				//4.判读是否需要进行进群欢迎
				if(gograde!=null) {
					GradeUserGoto GradeUserGoto = gotoservice.query().eq("qunnumber", msg.getGroup()).eq("type","gograde").one();
					if(GradeUserGoto.getInputs()!=null) {
						msgsender.SENDER.sendGroupMsg(msg.getGroup(), HtmlAndQq.msggotograde(GradeUserGoto.getInputs(),cqCodeUtil,msg));
					}
				}
			}
			
		} catch (Exception e) {
			msgsender.SENDER.sendPrivateMsg("936642284","酷Q机器人发生错误，位置在[用户加群欢送语，修改群名片]!QQ群："+msg.getGroup()+"！时间在："+TimeUtils.getStringDate() );
		}
		
	}
	/****
	 * 用户退群欢送语
	 * @param msg
	 * @param msgsender
	 */
	@Listen(MsgGetTypes.groupMemberReduce)
	@Filter
	public void to(GroupMemberReduce msg,MsgSender msgsender,CQCodeUtil cqCodeUtil) {
		try {
			GradeAdmin one = gService.query().eq("gradenumber", msg.getGroup()).eq("codes", "robot").eq("isgoto", "1").one();
			if(one!=null) {
				GradeAdmin one2 = gService.query().eq("gradenumber", msg.getGroup()).eq("codes", "tograde").eq("isgoto", "1").one();
				if(one2!=null) {
					GradeUserGotoService gotoservice = SpringContextUtil.getBean(GradeUserGotoService.class);
					GradeUserGoto GradeUserGoto = gotoservice.query().eq("qunnumber", msg.getGroup()).eq("type","tograde").one();
					if(GradeUserGoto.getInputs()!=null) {
						msgsender.SENDER.sendGroupMsg(msg.getGroup(), HtmlAndQq.groupMemberReduce(GradeUserGoto.getInputs(),cqCodeUtil,msg));
					}
				}
			}
		} catch (Exception e) {
			msgsender.SENDER.sendPrivateMsg("936642284","酷Q机器人发生错误，位置在[用户退群欢送语]!QQ群："+msg.getGroup()+"！时间在："+TimeUtils.getStringDate() );
		}
		
	}
}
